﻿Imports System.Xml.Serialization

Namespace Commands

    Public Class GetShares

        Public Class Request
        End Class
        Public Class Reply
            <XmlAttribute()> Public InReplyToMessageId As Integer
            Public Groups As New Generic.List(Of Group)
        End Class
        Public Class Group
            <XmlAttribute()> Public Name As String
            Public Shares As New Generic.List(Of Entities.SharedWatchFolder)
            Public Filters As New Generic.List(Of Filter)
            Public Sub New()
            End Sub
            Public Sub New(ByVal name As String)
                Me.Name = name
            End Sub
        End Class
        Public Class Filter
            <XmlAttribute()> Public Code As String
            <XmlText()> Public Name As String
            Public Sub New()
            End Sub
            Public Sub New(ByVal code As String, ByVal name As String)
                Me.Code = code : Me.Name = name
            End Sub
        End Class

        Friend Shared Function SendRequest(ByVal client As NetNode.Client) As Integer
            Dim clsRequest As New Request
            Dim strMessage As String = "GetShares"
            Dim aData() As Byte = Utils.Serialization.ToXmlBytes(clsRequest)
            Return client.SendMessageToTargetAsync(Manager.TargetName, strMessage, aData)
        End Function

        Friend Shared Sub HandleRequest(ByVal client As NetNode.Client, ByVal message As NetNode.MessageBlock)
            Dim aShares As Generic.List(Of Entities.SharedWatchFolder)
            aShares = DataStore.SharedWatchFolders.GetAll

            Dim clsReply As New Reply
            clsReply.InReplyToMessageId = message.Id

            Dim clsGroup As New Group("Shares")
            clsReply.Groups.Add(clsGroup)
            clsGroup.Shares = aShares

            clsGroup = New Group("Filters")
            clsReply.Groups.Add(clsGroup)
            clsGroup.Filters.Add(New Filter("\RecentAdditions", "Recent additions"))

            Dim bData() As Byte = Utils.Serialization.ToXmlBytes(clsReply)
            client.SendMessageToTargetAsync(Manager.TargetName, "GetSharesReply", bData)
        End Sub

        Friend Shared Sub HandleReply(ByVal client As NetNode.Client, ByVal message As NetNode.MessageBlock)
            Utils.Threading.Invoke(AddressOf HandleReply_CallBack, client, message)
        End Sub
        Private Shared Sub HandleReply_CallBack(ByVal client As NetNode.Client, ByVal message As NetNode.MessageBlock)
            Dim clsReply As Reply = Utils.Serialization.FromXmlBytes(Of Reply)(message.BinaryData)
            Manager.OnGetSharesReply(client, clsReply)
        End Sub

    End Class


End Namespace
